package com.springsecurity.security.oauth;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.NoSuchClientException;
import org.springframework.security.oauth2.provider.client.BaseClientDetails;
import org.springframework.stereotype.Service;

/**
 * <h3>Spring-Security</h3>
 * <p>redis客户端需要</p>
 *
 * @author gdw
 * @since 2024-07-17 09:55:46
 */
@Service
public class RedisClientDetailsService implements ClientDetailsService {

    @Autowired
    private RedisTemplate redisTemplate;

    private static final String CLIENT_KEY_PREFIX = "oauth:client:";

    @Override
    public ClientDetails loadClientByClientId(String clientId) throws NoSuchClientException {
        BaseClientDetails clientDetails = (BaseClientDetails) redisTemplate.opsForValue().get(CLIENT_KEY_PREFIX + clientId);
        if (clientDetails == null) {
            throw new NoSuchClientException("No client with requested id: " + clientId);
        }
        return clientDetails;
    }
}
